package com.yh.doing.interpret_pattern;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

/**
 * @program: design-patterns
 * @description: ${description}
 * @author: yangh0597@163.com
 * @date: 2021-03-09 18:23
 * @problem:
 **/
public class Main {
    public static void main(String[] args) throws Exception {

        String expStr = getExpStr();
        Context value = getValue(expStr);
        CalCulator calculator = new CalCulator(expStr);
        System.out.println("运算结果：" + expStr + "=" + calculator.run(value));


    }


    //获得值映射
    public static Context getValue(String expStr) throws IOException {
        HashMap<String, Integer> map = new HashMap<>();

        for (char ch : expStr.toCharArray()) {
            if (ch != '+' && ch != '-') {
                if (!map.containsKey(String.valueOf(ch))) {
                    System.out.print("请输入" + String.valueOf(ch) + "的值：");
                    String in = (new BufferedReader(new InputStreamReader(System.in))).readLine();
                    map.put(String.valueOf(ch), Integer.valueOf(in));
                }
            }
        }
        Context context = new Context(map);
        return context;
    }


    //获得表达式
    public static String getExpStr() throws IOException {
        System.out.print("请输入表达式：");
        return (new BufferedReader(new InputStreamReader(System.in))).readLine();
    }
}
